﻿Public Interface IContentCreator

    Function create() As IContent

End Interface

Public Class BaseContentCreator(Of T As {IContent, New})
    Implements IContentCreator

    Public Function create() As IContent Implements IContentCreator.create
        Return New T
    End Function
End Class


Public Class ContentCreatorAttribute
    Inherits MysteryDefaultClassAttribute

    Public Property implementing_type As Type


    Dim generic_impl As Type = GetType(BaseContentCreator(Of ))
    Dim generic_int As Type = GetType(IContentCreator)
    Dim _activator As IActivator

    Public Overrides Sub setUp()

        If implementing_type IsNot Nothing Then
            implementing_type.GetInterfaceMap(generic_int)
        Else
            implementing_type = generic_impl.MakeGenericType(used_in)
        End If

        _activator = getGlobalObject(Of FastActivator).getActivator(implementing_type)

    End Sub

    Public Function getCreator() As IContentCreator
        Return _activator.createInstance
    End Function

End Class

<GlobalAvalibleObjectImplementation(implementation_of:=GetType(IGlobalContentCreator), overrides_exsisting:=False, singleton:=True)>
Public Class GlobalContentCreator
    Implements IGlobalContentCreator

    Private _creators As New Dictionary(Of Type, IContentCreator)

    Public Function getNewContent(Of T As IContent)() As T Implements IGlobalContentCreator.getNewContent
        Return Me.getNewContent(GetType(T))
    End Function

    Public Function getNewContent(type As Type) As IContent Implements IGlobalContentCreator.getNewContent
        If Not _creators.ContainsKey(type) Then
            _creators(type) = type.getMysteryAttribute(Of ContentCreatorAttribute)().getCreator()
        End If
        Return _creators(type).create
    End Function

End Class